using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;
using FreeFlow;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for UserControl.
	/// </summary>
	public class UserControl : AdminUserControl
	{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox nameLabel;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox emailLabel;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.CheckedListBox rolesListBox;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.ComboBox reportsToComboBox;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.TextBox distinguishedNameTextBox;
    private System.Windows.Forms.TabPage toDoListPage;
    private System.Windows.Forms.TabPage watchListPage;
    private System.Windows.Forms.TabPage errorsPage;
    private ExtendedDataGrid historyGrid;
    private System.Windows.Forms.TabPage historyPage;
    private FreeFlowAdministrator.DesignerLogControl designerLogControl;
    private FreeFlowAdministrator.AlertsControl toDoList;
    private FreeFlowAdministrator.AlertsControl watchList;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.PictureBox blankPasswordImage;
    private System.Windows.Forms.Label blankPasswordLabel;
    private System.Windows.Forms.TextBox directoryTreeTextBox;
    private System.Windows.Forms.Label label6;
    private System.Windows.Forms.CheckBox deliverAlertsByEmailCheck;
    private System.Windows.Forms.TabControl tabControl;
    private System.Windows.Forms.TabPage attributesPage;
    private FreeFlowAdministrator.ExtendedDataGrid attributesGrid;
    private Panel panel1;
    private Panel panel2;
    private Panel rolesPanel;
    private Splitter splitter;
    private RealPosition realPosition;
    private Panel detailsPanel;
    private IContainer components;

    public UserControl()
    {
      // This call is required by the Windows.Forms Form Designer.
      InitializeComponent();

      ShowActions();
    }

    private void ShowActions()
    {
      UIActionList actionList = new UIActionList();
      actionList.Add("Apply changes", "Apply any changes made", new ActionExecuteDelegate(ApplyChanges));
      actionList.Add("Refresh", "Refresh the details for this user", new ActionExecuteDelegate(RefreshUser));
      actionList.Add("Delete", "Delete this user", new ActionExecuteDelegate(DeleteUser));
      actionList.Add("Change password", "Change the user's password", 
        new ActionExecuteDelegate(ChangePassword));
      actionList.Add("Set random password", "Give the user a random password", 
        new ActionExecuteDelegate(SetRandomPassword));
      
      if (tabControl.SelectedTab == errorsPage)
      {
        actionList.Add("Errors");
        actionList.Add(designerLogControl.ActionList);
      }
      else if (tabControl.SelectedTab == historyPage)
      {
        actionList.Add("History");
        actionList.Add("Open selected folders", "Open the selected folders", 
          new ActionExecuteDelegate(OpenSelected));
        actionList.Add("Copy to clipboard", "Copy the history to the clipboard", 
          new ActionExecuteDelegate(CopyToClipboard));
      }
      else if (tabControl.SelectedTab == toDoListPage)
      {
        actionList.Add("To Do");
        actionList.Add(toDoList.ActionList);
      }
      else if (tabControl.SelectedTab == watchListPage)
      {
        actionList.Add("Watch");
        actionList.Add(watchList.ActionList);
      }
      else if (tabControl.SelectedTab == attributesPage)
      {
        actionList.Add("Attributes");
        actionList.Add("Add attribute", "Add a new attribute", new ActionExecuteDelegate(AddAttribute));
        actionList.Add("Edit attribute", "Edit the selected attribute", 
          new ActionExecuteDelegate(EditAttribute));
        actionList.Add("Delete selected attributes", "Delete the selected attributes", 
          new ActionExecuteDelegate(DeleteSelectedAttributes));
      }
      
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
      realPosition.SaveControlLocations();
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UserControl));
      this.label1 = new System.Windows.Forms.Label();
      this.nameLabel = new System.Windows.Forms.TextBox();
      this.pictureBox1 = new System.Windows.Forms.PictureBox();
      this.label2 = new System.Windows.Forms.Label();
      this.emailLabel = new System.Windows.Forms.TextBox();
      this.label3 = new System.Windows.Forms.Label();
      this.rolesListBox = new System.Windows.Forms.CheckedListBox();
      this.label4 = new System.Windows.Forms.Label();
      this.reportsToComboBox = new System.Windows.Forms.ComboBox();
      this.label5 = new System.Windows.Forms.Label();
      this.distinguishedNameTextBox = new System.Windows.Forms.TextBox();
      this.tabControl = new System.Windows.Forms.TabControl();
      this.toDoListPage = new System.Windows.Forms.TabPage();
      this.toDoList = new FreeFlowAdministrator.AlertsControl();
      this.watchListPage = new System.Windows.Forms.TabPage();
      this.watchList = new FreeFlowAdministrator.AlertsControl();
      this.historyPage = new System.Windows.Forms.TabPage();
      this.historyGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.errorsPage = new System.Windows.Forms.TabPage();
      this.designerLogControl = new FreeFlowAdministrator.DesignerLogControl();
      this.attributesPage = new System.Windows.Forms.TabPage();
      this.attributesGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.blankPasswordLabel = new System.Windows.Forms.Label();
      this.blankPasswordImage = new System.Windows.Forms.PictureBox();
      this.directoryTreeTextBox = new System.Windows.Forms.TextBox();
      this.label6 = new System.Windows.Forms.Label();
      this.deliverAlertsByEmailCheck = new System.Windows.Forms.CheckBox();
      this.panel1 = new System.Windows.Forms.Panel();
      this.panel2 = new System.Windows.Forms.Panel();
      this.rolesPanel = new System.Windows.Forms.Panel();
      this.splitter = new System.Windows.Forms.Splitter();
      this.realPosition = new FreeFlow.WinFormsControls.HelperClasses.RealPosition(this.components);
      this.detailsPanel = new System.Windows.Forms.Panel();
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
      this.tabControl.SuspendLayout();
      this.toDoListPage.SuspendLayout();
      this.watchListPage.SuspendLayout();
      this.historyPage.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.historyGrid)).BeginInit();
      this.errorsPage.SuspendLayout();
      this.attributesPage.SuspendLayout();
      ((System.ComponentModel.ISupportInitialize)(this.attributesGrid)).BeginInit();
      ((System.ComponentModel.ISupportInitialize)(this.blankPasswordImage)).BeginInit();
      this.panel1.SuspendLayout();
      this.panel2.SuspendLayout();
      this.rolesPanel.SuspendLayout();
      this.detailsPanel.SuspendLayout();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.Location = new System.Drawing.Point(8, 7);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(40, 16);
      this.label1.TabIndex = 0;
      this.label1.Text = "Name:";
      // 
      // nameLabel
      // 
      this.nameLabel.Location = new System.Drawing.Point(120, 7);
      this.nameLabel.Name = "nameLabel";
      this.nameLabel.Size = new System.Drawing.Size(240, 20);
      this.nameLabel.TabIndex = 1;
      this.nameLabel.Text = "nameLabel";
      // 
      // pictureBox1
      // 
      this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
      this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
      this.pictureBox1.Location = new System.Drawing.Point(620, 7);
      this.pictureBox1.Name = "pictureBox1";
      this.pictureBox1.Size = new System.Drawing.Size(32, 32);
      this.pictureBox1.TabIndex = 2;
      this.pictureBox1.TabStop = false;
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(8, 31);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(40, 16);
      this.label2.TabIndex = 3;
      this.label2.Text = "Email:";
      // 
      // emailLabel
      // 
      this.emailLabel.Location = new System.Drawing.Point(120, 31);
      this.emailLabel.Name = "emailLabel";
      this.emailLabel.Size = new System.Drawing.Size(240, 20);
      this.emailLabel.TabIndex = 4;
      this.emailLabel.Text = "emailLabel";
      // 
      // label3
      // 
      this.label3.Location = new System.Drawing.Point(3, 3);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(105, 19);
      this.label3.TabIndex = 6;
      this.label3.Text = "Roles for this user:";
      // 
      // rolesListBox
      // 
      this.rolesListBox.CheckOnClick = true;
      this.rolesListBox.Dock = System.Windows.Forms.DockStyle.Fill;
      this.rolesListBox.Location = new System.Drawing.Point(0, 22);
      this.rolesListBox.Name = "rolesListBox";
      this.rolesListBox.Size = new System.Drawing.Size(152, 304);
      this.rolesListBox.TabIndex = 7;
      // 
      // label4
      // 
      this.label4.Location = new System.Drawing.Point(8, 55);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(64, 16);
      this.label4.TabIndex = 8;
      this.label4.Text = "Reports to:";
      // 
      // reportsToComboBox
      // 
      this.reportsToComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.reportsToComboBox.Location = new System.Drawing.Point(120, 55);
      this.reportsToComboBox.Name = "reportsToComboBox";
      this.reportsToComboBox.Size = new System.Drawing.Size(240, 21);
      this.reportsToComboBox.TabIndex = 9;
      // 
      // label5
      // 
      this.label5.Location = new System.Drawing.Point(8, 79);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(112, 16);
      this.label5.TabIndex = 10;
      this.label5.Text = "Distinguished name:";
      // 
      // distinguishedNameTextBox
      // 
      this.distinguishedNameTextBox.Location = new System.Drawing.Point(120, 79);
      this.distinguishedNameTextBox.Name = "distinguishedNameTextBox";
      this.distinguishedNameTextBox.Size = new System.Drawing.Size(240, 20);
      this.distinguishedNameTextBox.TabIndex = 11;
      this.distinguishedNameTextBox.Text = "distinguishedNameTextBox";
      // 
      // tabControl
      // 
      this.tabControl.Controls.Add(this.toDoListPage);
      this.tabControl.Controls.Add(this.watchListPage);
      this.tabControl.Controls.Add(this.historyPage);
      this.tabControl.Controls.Add(this.errorsPage);
      this.tabControl.Controls.Add(this.attributesPage);
      this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.tabControl.Location = new System.Drawing.Point(0, 0);
      this.tabControl.Name = "tabControl";
      this.tabControl.SelectedIndex = 0;
      this.tabControl.Size = new System.Drawing.Size(503, 331);
      this.tabControl.TabIndex = 12;
      this.tabControl.SelectedIndexChanged += new System.EventHandler(this.tabControl_SelectedIndexChanged);
      // 
      // toDoListPage
      // 
      this.toDoListPage.Controls.Add(this.toDoList);
      this.toDoListPage.Location = new System.Drawing.Point(4, 22);
      this.toDoListPage.Name = "toDoListPage";
      this.toDoListPage.Size = new System.Drawing.Size(495, 305);
      this.toDoListPage.TabIndex = 0;
      this.toDoListPage.Text = "To Do";
      // 
      // toDoList
      // 
      this.toDoList.Dock = System.Windows.Forms.DockStyle.Fill;
      this.toDoList.Location = new System.Drawing.Point(0, 0);
      this.toDoList.Name = "toDoList";
      this.toDoList.Size = new System.Drawing.Size(495, 305);
      this.toDoList.TabIndex = 0;
      // 
      // watchListPage
      // 
      this.watchListPage.Controls.Add(this.watchList);
      this.watchListPage.Location = new System.Drawing.Point(4, 22);
      this.watchListPage.Name = "watchListPage";
      this.watchListPage.Size = new System.Drawing.Size(495, 305);
      this.watchListPage.TabIndex = 1;
      this.watchListPage.Text = "Watch";
      // 
      // watchList
      // 
      this.watchList.Dock = System.Windows.Forms.DockStyle.Fill;
      this.watchList.Location = new System.Drawing.Point(0, 0);
      this.watchList.Name = "watchList";
      this.watchList.Size = new System.Drawing.Size(495, 305);
      this.watchList.TabIndex = 0;
      // 
      // historyPage
      // 
      this.historyPage.Controls.Add(this.historyGrid);
      this.historyPage.Location = new System.Drawing.Point(4, 22);
      this.historyPage.Name = "historyPage";
      this.historyPage.Size = new System.Drawing.Size(495, 305);
      this.historyPage.TabIndex = 2;
      this.historyPage.Text = "History";
      // 
      // historyGrid
      // 
      this.historyGrid.AllowNavigation = false;
      this.historyGrid.CaptionVisible = false;
      this.historyGrid.DataMember = "";
      this.historyGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.historyGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.historyGrid.Location = new System.Drawing.Point(0, 0);
      this.historyGrid.Name = "historyGrid";
      this.historyGrid.Size = new System.Drawing.Size(495, 305);
      this.historyGrid.TabIndex = 0;
      this.historyGrid.OpenSelected += new System.EventHandler(this.historyGrid_OpenSelected);
      // 
      // errorsPage
      // 
      this.errorsPage.Controls.Add(this.designerLogControl);
      this.errorsPage.Location = new System.Drawing.Point(4, 22);
      this.errorsPage.Name = "errorsPage";
      this.errorsPage.Size = new System.Drawing.Size(495, 305);
      this.errorsPage.TabIndex = 3;
      this.errorsPage.Text = "Errors";
      // 
      // designerLogControl
      // 
      this.designerLogControl.ActionsVisible = false;
      this.designerLogControl.Dock = System.Windows.Forms.DockStyle.Fill;
      this.designerLogControl.Location = new System.Drawing.Point(0, 0);
      this.designerLogControl.Name = "designerLogControl";
      this.designerLogControl.Size = new System.Drawing.Size(495, 305);
      this.designerLogControl.TabIndex = 0;
      // 
      // attributesPage
      // 
      this.attributesPage.Controls.Add(this.attributesGrid);
      this.attributesPage.Location = new System.Drawing.Point(4, 22);
      this.attributesPage.Name = "attributesPage";
      this.attributesPage.Size = new System.Drawing.Size(495, 305);
      this.attributesPage.TabIndex = 4;
      this.attributesPage.Text = "Attributes";
      // 
      // attributesGrid
      // 
      this.attributesGrid.AllowNavigation = false;
      this.attributesGrid.CaptionVisible = false;
      this.attributesGrid.DataMember = "";
      this.attributesGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.attributesGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.attributesGrid.Location = new System.Drawing.Point(0, 0);
      this.attributesGrid.Name = "attributesGrid";
      this.attributesGrid.Size = new System.Drawing.Size(495, 305);
      this.attributesGrid.TabIndex = 0;
      this.attributesGrid.OpenSelected += new System.EventHandler(this.attributesGrid_OpenSelected);
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(658, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 512);
      this.uiActionsControl.TabIndex = 13;
      // 
      // blankPasswordLabel
      // 
      this.blankPasswordLabel.Location = new System.Drawing.Point(32, 159);
      this.blankPasswordLabel.Name = "blankPasswordLabel";
      this.blankPasswordLabel.Size = new System.Drawing.Size(168, 16);
      this.blankPasswordLabel.TabIndex = 33;
      this.blankPasswordLabel.Text = "This user has a blank password";
      // 
      // blankPasswordImage
      // 
      this.blankPasswordImage.Image = ((System.Drawing.Image)(resources.GetObject("blankPasswordImage.Image")));
      this.blankPasswordImage.Location = new System.Drawing.Point(8, 159);
      this.blankPasswordImage.Name = "blankPasswordImage";
      this.blankPasswordImage.Size = new System.Drawing.Size(16, 16);
      this.blankPasswordImage.TabIndex = 32;
      this.blankPasswordImage.TabStop = false;
      // 
      // directoryTreeTextBox
      // 
      this.directoryTreeTextBox.Location = new System.Drawing.Point(120, 103);
      this.directoryTreeTextBox.Name = "directoryTreeTextBox";
      this.directoryTreeTextBox.Size = new System.Drawing.Size(240, 20);
      this.directoryTreeTextBox.TabIndex = 34;
      this.directoryTreeTextBox.Text = "directoryTreeTextBox";
      // 
      // label6
      // 
      this.label6.Location = new System.Drawing.Point(8, 105);
      this.label6.Name = "label6";
      this.label6.Size = new System.Drawing.Size(100, 16);
      this.label6.TabIndex = 35;
      this.label6.Text = "Directory tree:";
      // 
      // deliverAlertsByEmailCheck
      // 
      this.deliverAlertsByEmailCheck.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.deliverAlertsByEmailCheck.Location = new System.Drawing.Point(10, 127);
      this.deliverAlertsByEmailCheck.Name = "deliverAlertsByEmailCheck";
      this.deliverAlertsByEmailCheck.Size = new System.Drawing.Size(142, 24);
      this.deliverAlertsByEmailCheck.TabIndex = 0;
      this.deliverAlertsByEmailCheck.Text = "Deliver alerts by email";
      // 
      // panel1
      // 
      this.panel1.Controls.Add(this.reportsToComboBox);
      this.panel1.Controls.Add(this.deliverAlertsByEmailCheck);
      this.panel1.Controls.Add(this.label1);
      this.panel1.Controls.Add(this.label6);
      this.panel1.Controls.Add(this.nameLabel);
      this.panel1.Controls.Add(this.directoryTreeTextBox);
      this.panel1.Controls.Add(this.pictureBox1);
      this.panel1.Controls.Add(this.blankPasswordLabel);
      this.panel1.Controls.Add(this.label2);
      this.panel1.Controls.Add(this.blankPasswordImage);
      this.panel1.Controls.Add(this.emailLabel);
      this.panel1.Controls.Add(this.label4);
      this.panel1.Controls.Add(this.label5);
      this.panel1.Controls.Add(this.distinguishedNameTextBox);
      this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel1.Location = new System.Drawing.Point(0, 0);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(658, 181);
      this.panel1.TabIndex = 36;
      // 
      // panel2
      // 
      this.panel2.Controls.Add(this.label3);
      this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
      this.panel2.Location = new System.Drawing.Point(0, 0);
      this.panel2.Name = "panel2";
      this.panel2.Size = new System.Drawing.Size(152, 22);
      this.panel2.TabIndex = 8;
      // 
      // rolesPanel
      // 
      this.rolesPanel.Controls.Add(this.rolesListBox);
      this.rolesPanel.Controls.Add(this.panel2);
      this.rolesPanel.Dock = System.Windows.Forms.DockStyle.Left;
      this.rolesPanel.Location = new System.Drawing.Point(0, 181);
      this.rolesPanel.MinimumSize = new System.Drawing.Size(100, 0);
      this.rolesPanel.Name = "rolesPanel";
      this.realPosition.SetRestoreLocation(this.rolesPanel, true);
      this.rolesPanel.Size = new System.Drawing.Size(152, 331);
      this.rolesPanel.TabIndex = 37;
      // 
      // splitter
      // 
      this.splitter.Location = new System.Drawing.Point(152, 181);
      this.splitter.Name = "splitter";
      this.realPosition.SetRestoreLocation(this.splitter, true);
      this.splitter.Size = new System.Drawing.Size(3, 331);
      this.splitter.TabIndex = 38;
      this.splitter.TabStop = false;
      // 
      // realPosition
      // 
      this.realPosition.Parent = null;
      this.realPosition.RegistryPath = "Software\\FreeFlow\\Administrator\\UserControl";
      // 
      // detailsPanel
      // 
      this.detailsPanel.Controls.Add(this.tabControl);
      this.detailsPanel.Dock = System.Windows.Forms.DockStyle.Fill;
      this.detailsPanel.Location = new System.Drawing.Point(155, 181);
      this.detailsPanel.Name = "detailsPanel";
      this.detailsPanel.Size = new System.Drawing.Size(503, 331);
      this.detailsPanel.TabIndex = 39;
      // 
      // UserControl
      // 
      this.Controls.Add(this.detailsPanel);
      this.Controls.Add(this.splitter);
      this.Controls.Add(this.rolesPanel);
      this.Controls.Add(this.panel1);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "UserControl";
      this.Size = new System.Drawing.Size(808, 512);
      this.ParentChanged += new System.EventHandler(this.UserControl_ParentChanged);
      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
      this.tabControl.ResumeLayout(false);
      this.toDoListPage.ResumeLayout(false);
      this.watchListPage.ResumeLayout(false);
      this.historyPage.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.historyGrid)).EndInit();
      this.errorsPage.ResumeLayout(false);
      this.attributesPage.ResumeLayout(false);
      ((System.ComponentModel.ISupportInitialize)(this.attributesGrid)).EndInit();
      ((System.ComponentModel.ISupportInitialize)(this.blankPasswordImage)).EndInit();
      this.panel1.ResumeLayout(false);
      this.panel1.PerformLayout();
      this.panel2.ResumeLayout(false);
      this.rolesPanel.ResumeLayout(false);
      this.detailsPanel.ResumeLayout(false);
      this.ResumeLayout(false);

    }
		#endregion

    private User user;
    public void LoadUser(User user)
    {
      this.user = user;
      nameLabel.Text = user.Name;
      emailLabel.Text = user.EmailAddress;
      distinguishedNameTextBox.Text = user.DistinguishedName;
      deliverAlertsByEmailCheck.Checked = user.DeliverAlertsByEmail;
      directoryTreeTextBox.Text = user.DirectoryTree;

      // reports to
      reportsToComboBox.Items.Clear();
      reportsToComboBox.Items.Add(string.Empty);
      UserCollection users = user.Server.Users;
      for (int i = 0; i < users.Count; i++)
      {
        if (users[i].Name != user.Name)
          reportsToComboBox.Items.Add(users[i].Name);
      }
      reportsToComboBox.Text = user.ReportsTo;

      // show all available roles
      rolesListBox.Items.Clear();
      RoleCollection roles = user.Server.Roles;
      for (int i = 0; i < roles.Count; i++)
      {
        rolesListBox.Items.Add(roles[i].Name);
        if (user.HasRole(roles[i].Name))
          rolesListBox.SetItemChecked(i, true);
      }

      LoadGrid();

      // blank password
      HideShowBlankPassword();
    }

    private void HideShowBlankPassword()
    {
      blankPasswordImage.Visible = user.HasBlankPassword;
      blankPasswordLabel.Visible = user.HasBlankPassword;
    }

    private string ApplyChanges()
    {
      using (new CursorHandler())
      {
        user.Name = nameLabel.Text;
        user.EmailAddress = emailLabel.Text;
        user.ReportsTo = reportsToComboBox.Text;
        user.DistinguishedName = distinguishedNameTextBox.Text;
        user.DeliverAlertsByEmail = deliverAlertsByEmailCheck.Checked;
        user.DirectoryTree = directoryTreeTextBox.Text;
        user.RevokeAllRoles();
        for (int i = 0; i < rolesListBox.Items.Count; i++)
        {
          if (rolesListBox.GetItemChecked(i))
            user.GrantRole(rolesListBox.Items[i].ToString());
        }
        user.ApplyChanges();
        OnRefreshRequired();
        return Constants.ChangesApplied;
      }
    }

    private string OpenSelected()
    {
      using (new CursorHandler())
      {
        CurrencyManager bm = (CurrencyManager)historyGrid.BindingContext[historyGrid.DataSource];
        for (int i = 0; i < bm.Count; i++) 
        { 
          if(historyGrid.IsSelected(i))
          {
            HistoryItem historyItem = (HistoryItem)bm.List[i];
            Folder folder = new Folder(user.Server, historyItem.FolderId);
            FolderForm form = new FolderForm();
            form.LoadFolder(folder);
            form.Show();
          }
        }
      }
      return null;
    }

    private string CopyToClipboard()
    {
      historyGrid.CopyToClipboard();
      return null;
    }

    private string ChangePassword()
    {
      string password = InputBox.ShowInputBox("Change Password", "", true);
      if (password != null)
      {
        user.SetPassword(password);
        HideShowBlankPassword();
      }
      return null;
    }

    private string SetRandomPassword()
    {
      string password = user.SetRandomPassword();
      HideShowBlankPassword();
      InputBox.ShowInputBox("Password has been set to", password, false, false);
      return null;
    }

    private string RefreshUser()
    {
      try
      {
        user.Refresh();
        user.Server.Roles.Refresh();
        user.Server.Users.Refresh();
        LoadUser(user);
      }
      catch (ItemDeletedException e)
      {
        MessageBox.Show(e.Message);
        OnItemDeleted();
        Dispose();
      }
      return null;
    }

    private string DeleteUser()
    {
      if (MessageBox.Show("Delete this user?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
      {
        using (new CursorHandler())
        {
          user.Delete();
        }
        OnItemDeleted();
        Dispose();
      }
      return null;
    }

    private string AddAttribute()
    {
      using (AttributeForm form = new AttributeForm())
      {
        UserAttribute attribute = new UserAttribute(user.Server, user.Name);
        form.AttributeName = "";
        form.AttributeValue = "";
        if (form.ShowDialog() == DialogResult.OK)
        {
          attribute.Name = form.AttributeName;
          attribute.Value = form.AttributeValue;
          user.Attributes.Add(attribute);
          attributesGrid.RefreshGrid();
        }
      }
      return null;
    }

    private string EditAttribute()
    {
      if (attributesGrid.CurrentRowIndex > -1)
      {
        using (AttributeForm form = new AttributeForm())
        {
          CurrencyManager bm = (CurrencyManager)attributesGrid.BindingContext[attributesGrid.DataSource];
          UserAttribute attribute = (UserAttribute)bm.Current;
          form.AttributeName = attribute.Name;
          form.AttributeValue = attribute.Value;
          if (form.ShowDialog() == DialogResult.OK)
          {
            attribute.Name = form.AttributeName;
            attribute.Value = form.AttributeValue;
            attribute.ApplyChanges();
            attributesGrid.RefreshGrid();
          }
        }
      }
      return null;
    }

    private string DeleteSelectedAttributes()
    {
      using (new CursorHandler())
      {
        attributesGrid.DeleteSelected();
      }
      return null;
    }

    private void tabControl_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      LoadGrid();
    }

    private void LoadGrid()
    {
      using (new CursorHandler())
      {
        // only load grids as necessary
        if (tabControl.SelectedTab == errorsPage)
        {
          designerLogControl.LoadDesignerLog(user.DesignerLog);
        }
        else if (tabControl.SelectedTab == historyPage)
        {
          // history
          historyGrid.DataSource = user.History;
          historyGrid.ClearColumns();
          historyGrid.AddColumn("EventTime", "Event time");
          historyGrid.AddColumn("FolderId", "Folder ID");
          historyGrid.AddColumn("FromStage", "From stage");
          historyGrid.AddColumn("Action");
          historyGrid.AddColumn("ToStage", "To stage");
          historyGrid.AddColumn("Notes");
          historyGrid.AddColumn("Message");
          historyGrid.AddColumn("User");
          historyGrid.AddColumn("Version");
          historyGrid.AutosizeColumns();
        }
        else if (tabControl.SelectedTab == watchListPage)
        {
          watchList.LoadAlerts(user.WatchList);
        }
        else if (tabControl.SelectedTab == toDoListPage)
        {
          toDoList.LoadAlerts(user.ToDoList);
        }
        else if (tabControl.SelectedTab == attributesPage)
        {
          attributesGrid.DataSource = user.Attributes;
          attributesGrid.ClearColumns();
          attributesGrid.AddColumn("Name");
          attributesGrid.AddColumn("Value");
        }

        ShowActions(); 
      }
    }

    private void attributesGrid_OpenSelected(object sender, EventArgs e)
    {
      EditAttribute();
    }

    private void UserControl_ParentChanged(object sender, EventArgs e)
    {
      // do the load here to ensure the positions get loaded after a previous UserControl has been
      // disposed
      if (Parent != null)
      {
        realPosition.RestoreControlLocations();
      }
    }

    private void historyGrid_OpenSelected(object sender, EventArgs e)
    {
      OpenSelected();
    }
	}
}
